package ecnu.ecgbackend.controller;

import ecnu.ecgbackend.service.DiagnosisUserService;
import ecnu.ecgbackend.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin")
public class DiagnosisUserManageController {
    @Resource
    DiagnosisUserService diagnosisUserService;
    @PreAuthorize("hasAuthority('admin')")
    @GetMapping("/list")
    public Result queryUserList(
            @RequestParam("category") String category,
            @RequestParam("username") String username,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize){
        return Result.ok(diagnosisUserService.pageUserList(category,username,pageNum,pageSize));
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/doctor/authenticate")
    public Result passCertification(int id){
        if(diagnosisUserService.editDoctorCertifiedTrue(id)){
            return Result.ok().message("认证成功！");
        }
        return Result.error().message("认证失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("doctor/unauthenticate")
    public Result cancelCertification(int id){
        if(diagnosisUserService.editDoctorCertifiedFalse(id)){
            return Result.ok().message("取消认证成功！");
        }
        return Result.error().message("取消认证失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/lock")
    public Result lockAccount(int id){
        if(diagnosisUserService.editLockAccount(id)){
            return Result.ok().message("锁定成功！");
        }
        return Result.error().message("锁定失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/unlock")
    public Result unlockAccount(int id){
        if(diagnosisUserService.editUnlockAccount(id)){
            return Result.ok().message("解锁成功！");
        }
        return Result.error().message("解锁失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/enable")
    public Result enableAccount(int id){
        if(diagnosisUserService.editEnableAccount(id)){
            return Result.ok().message("启用成功！");
        }
        return Result.error().message("启用失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/disable")
    public Result disableAccount(int id){
        if(diagnosisUserService.editDisableAccount(id)){
            return Result.ok().message("禁用成功！");
        }
        return Result.error().message("禁用失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/logout")
    public Result logoutAccount(int id){
        if(diagnosisUserService.editLogoutAccount(id)){
            return Result.ok().message("注销成功！");
        }
        return Result.error().message("注销失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/restore")
    public Result restoreAccount(int id){
        if(diagnosisUserService.editRestoreAccount(id)){
            return Result.ok().message("恢复成功！");
        }
        return Result.error().message("恢复失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/accountExpired")
    public Result modifyAccountExpired(int id){
        if(diagnosisUserService.editAccountExpired(id)){
            return Result.ok().message("设置账号过期成功！");
        }
        return Result.error().message("设置账号过期失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/accountUnexpired")
    public Result modifyAccountUnexpired(int id){
        if(diagnosisUserService.editAccountUnexpired(id)){
            return Result.ok().message("恢复过期账号成功！");
        }
        return Result.error().message("恢复过期账号失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/passwordExpired")
    public Result modifyPasswordExpired(int id){
        if(diagnosisUserService.editPasswordExpired(id)){
            return Result.ok().message("设置密码过期成功！");
        }
        return Result.error().message("设置密码过期失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/passwordUnexpired")
    public Result modifyPasswordUnexpired(int id){
        if(diagnosisUserService.editPasswordUnexpired(id)){
            return Result.ok().message("恢复过期密码成功！");
        }
        return Result.error().message("恢复过期密码失败");
    }
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/password/reset")
    public Result resetPassword(int id,String password){
        return diagnosisUserService.editPasswordById(id,password);
    }
    @PostMapping("/integral")
    public Result modifyIntegral(int id,int integral){
        if(diagnosisUserService.editIntegral(id,integral)){
            return Result.ok().message("积分修改成功！");
        }
        return Result.error().message("积分修改失败");
    }
}
